﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo.moduloEditorial
{
    public class conjuntoEditoriales:conjuntoBase
    {
        #region "constructores"

        public conjuntoEditoriales(datosAplicacion dA)
            : base(dA)
        {
            this.numTipo = conjuntoBase.CONJEDITORIALES;
        }

        #endregion

        #region "metodos"

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }

        #region "imec"

        public void agregareditorial(editorial nuevoEditorial)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_editorial('" + nuevoEditorial.nombre + "')";

                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);

                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al agregar editorial: " + nuevoEditorial.nombre);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void eliminareditorial(String codeditorial)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_editorial('" + codeditorial + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch(Exception e)
            {
                throw new Exception(e.Message + "\nError al eliminar editorial: " + codeditorial);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public editorial obtenereditorial(String nombreEditorial)
        {
            editorial nueva = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_editorial_id('" + nombreEditorial + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 1);
                    nueva = new editorial(valores[0]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el editorial " + nombreEditorial);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {
                    
                }
            }
            return nueva;
        }

        public LinkedList<editorial> obtenerTodos()
        {
            LinkedList<editorial> nueva = new LinkedList<editorial>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_editorial()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 1);
                    nueva.AddLast(new editorial(valores[0]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener los editoriales ");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nueva;
        }

        #endregion

        #endregion
    }
}
